package com.ctshk.app.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.enums.SysAreaLevel;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.system.dto.AreaDTO;
import com.ctshk.rpc.system.req.AreaILevelReq;
import com.ctshk.rpc.system.req.AreaQueryLevelReq;
import com.ctshk.rpc.system.req.AreaQueryReq;
import com.ctshk.rpc.system.req.AreaQueryWhereReq;
import com.ctshk.rpc.system.service.ISysAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-11
 */
@Api(value = "国家区域-app", tags = "国家-app")
@RestController
@RequestMapping("/sys/area-app")
public class SysAreaController extends BaseController {

    public static final String CN = "4RS";
    public static final String TW = "CP4";
    public static final String XG = "824";
    public static final String AM = "7ID";

    @DubboReference
    private ISysAreaService areaService;

    @ApiOperation(value = "查询国家区域列表-app", notes = "查询国家区域列表-app")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @PostMapping("/query/likeList")
    public Result<List<AreaDTO>> queryList(@RequestBody @Valid AreaQueryReq areaQueryReq) {
        return Result.success(areaService.queryListEs(areaQueryReq));
    }

    @ApiOperation(value = "條件查询国家区域列表-app", notes = "條件查询国家区域列表-app")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @PostMapping("/query/queryListWhere")
    public Result<List<AreaDTO>> queryListWhere(@RequestBody @Valid AreaQueryWhereReq req) {
        return Result.success(areaService.queryListEsWhere(req));
    }

    @ApiOperation(value = "查询国家区域列表tree結構-app", notes = "查询国家区域列表tree結構-app")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @PostMapping("/query/queryTree")
    public Result<JSONArray> queryTree(@RequestBody @Valid AreaILevelReq req) {
        return Result.success(areaService.queryListEsByLevelTwo(req));
    }

    @ApiOperation(value = "查询国家区域列表level-app", notes = "查询国家区域列表level-app")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @PostMapping("/query/levelList")
    public Result<List<AreaDTO>> queryLevelList(@RequestBody @Valid AreaQueryLevelReq req) {
        return Result.success(areaService.queryListEsByLevel(req));
    }

    @ApiOperation(value = "查询国家区域列表level(带省级港澳台)-app", notes = "查询国家区域列表level(带省级港澳台)-app")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @PostMapping("/query/levelListTwo")
    public Result<List<AreaDTO>> queryLevelList2(@RequestBody @Valid AreaQueryLevelReq req) {
        List<AreaDTO> result = areaService.queryListEsByLevel(req);
        if (req.getType() == SysAreaLevel.TWO.getCode() && req.getLevel() == 1) {
            req.setType(SysAreaLevel.ONE.getCode());
            req.setLevel(1);
            List<AreaDTO> result2 = areaService.queryListEsByLevel(req);
            result2.forEach(e -> {
                //港澳台降级为2级
                if (!CN.equals(e.getCountryCode())) {
                    e.setProvinceCode(e.getCountryCode());
                    e.setProvinceEn(e.getCountryEn());
                    e.setProvincePinyin(e.getCountryPinyin());
                    e.setProvinceSimple(e.getCountrySimple());
                    e.setProvinceTraditional(e.getCountryTraditional());
                    result.add(e);
                }
            });
        }
        return Result.success(result);
    }

    @ApiOperation(value = "查询国家区域列表level(带省级港澳台的子级)-app", notes = "查询国家区域列表level(带省级港澳台的子级)-app")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @PostMapping("/query/levelListThree")
    public Result<List<AreaDTO>> queryLevelList3() {
        List<AreaDTO> result = new ArrayList<>();
        AreaQueryLevelReq req = new AreaQueryLevelReq();
        req.setType(SysAreaLevel.TWO.getCode());
        req.setPatenCode(XG);
        req.setLevel(1);
        List<AreaDTO> xgs = areaService.queryListEsByLevel(req);
        req.setPatenCode(AM);
        List<AreaDTO> ams = areaService.queryListEsByLevel(req);
        req.setPatenCode(TW);
        List<AreaDTO> tws = areaService.queryListEsByLevel(req);
        result.addAll(xgs);
        result.addAll(ams);
        result.addAll(tws);
        return Result.success(result);
    }

    @ApiOperation(value = "查询城市-app", notes = "查询城市-app")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @PostMapping("/queryById/{id}")
    public Result<AreaDTO> queryByEsId(@PathVariable("id") Long id) {
        return Result.success(areaService.queryByEsId(id));
    }

    @ApiOperation(value = "查询国家列表OneLevel-app", notes = "查询国家列表OneLevel-app")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @GetMapping("/query/esOneLevel")
    public Result<List<AreaDTO>> esOneLevel(@RequestParam("name") String name) {
        return Result.success(areaService.esOneLevel(name));
    }

    @ApiOperation(value = "初始化-app", notes = "初始化-app")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @PostMapping("/init")
    public Result updateElasticSearch() {
        areaService.updateElasticSearch();
        return Result.success();
    }

}
